package com.huangmb.anymark.server.dto;

import com.alibaba.fastjson.JSON;

/**
 * http响应实体
 * @author hmb
 * @version 1.0
 * @since 2016/5/3 20:22
 */
public class Response {
    public static final int SUCCESS = 0;
    public static final int CODE_AUTH_FAIL = 1;
    public static final int CODE_NAME_OR_PWD_ERROR = 2;
    public static final int CODE_NAME_EXIST = 3;
    public static final int CODE_BAD_PARAMETER = 4;

    public static final String MSG_AUTH_FAIL = "未登录";
    public static final String MSG_NAME_OR_PWD_ERROR = "用户名或密码错误";
    public static final String MSG_NAME_EXIST = "用户名已存在";
    public static final String MSG_BAD_PARAMETER = "参数错误";

    private int errorCode = -1;
    private String errorMessage;
    private String result;

    public Response(){

    }
    public Response(int code,String msg,String result){
        this.errorCode = code;
        this.errorMessage = msg;
        this.result = result;
    }
    public static Response parse(String response){
        try{
            return JSON.parseObject(response,Response.class);
        }catch (Exception e){
            return null;
        }

    }

    public boolean hasError(){
        return errorCode != 0;
    }

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public static Response buildSuccess(String result){
        return new Response(0,"",result);
    }
}
